package com.larly.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.larly.model.domain.PrivateMessage;
import com.larly.service.PrivateMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 定时任务：清理30天前的过期私信（每日凌晨1点执行）
 *
 * @author larly
 */
@Slf4j
@Component
public class ClearTask {

    @Resource
    private PrivateMessageService privateMessageService;

    @Scheduled(cron = "0 0 1 * * ?")
    public void cleanExpiredMessages() {
        LambdaQueryWrapper<PrivateMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(PrivateMessage::getExpireTime, new Date()); // 过期时间≤当前时间
        privateMessageService.remove(queryWrapper);
        log.info("清理过期私信完成，删除数量：{}", privateMessageService.count(queryWrapper));
    }
}
